import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_framework/app_inject.dart';
import 'package:flutter_framework/routes/home/home_routes.dart';
import 'package:flutter_framework/routes/home/indexRouter.dart';
import 'package:flutter_framework/theme/theme_ctrl.dart';
import 'package:flutter_framework/theme/theme_item.dart';
import 'package:flutter_framework/translation/translation.dart';
import 'package:flutter_framework/utils/app_config/app_env.dart';
import 'package:flutter_framework/utils/http/base_url_config.dart';
import 'package:flutter_framework/utils/http/dio_utils.dart';
import 'package:flutter_framework/utils/logs.dart';
import 'package:flutter_framework/views/custom_provider/model/logo_model.dart';
import 'package:flutter_framework/views/debug/debug_view.dart';
import 'package:flutter_framework/views/home/home.dart';
import 'package:flutter_framework/views/not_found/not_found_page.dart';
import 'package:flutter_framework/views/test_inheritwidget/test_inheritwidget.dart';
import 'package:flutter_portal/flutter_portal.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart' as riverpod;
import 'package:get/get.dart';

import 'views/custom_provider/model/change_notifier_provider.dart';

void main() async {
  await mainInit();
}

Future<void> mainInit() async {
  /// 初始化环境
  BaseUrlUtils.setConfig(AppEnv.release);
  // 初始化主题控制器,有可能缓存设置过主题，所以提前初始化
  Get.put(ThemeCtrl(), permanent: true);

  /// 进行dio全局的初始化
  DioUtils.initDio();

  // SystemChrome.setEnabledSystemUIOverlays([]); // 全屏显示
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    // 如果不需要扩展颜色，直接用 themeData定义就好了 getbuilder 也不需要加
    return riverpod.ProviderScope(
      child: ThemeColorBox(
        color: Colors.red,
        child:
            // 包裹一个任意门根组件
            Portal(
          child:
              // 注入一个自定义的provider
              ChangeNotifierProvider(
            create: () => LogoModel(),
            child: GetBuilder(
              builder: (ThemeCtrl ctrl) => GetMaterialApp(
                  title: 'Flutter Demo',
                  // 初始化注入依赖
                  initialBinding: AppInjector(),
                  theme: ctrl.themeItem.themeData,
                  themeMode: ctrl.themeItem.themeMode,
                  // 定义统一的路由过渡动画
                  defaultTransition: Transition.rightToLeft,
                  translations: LanguageSource(),

                  // 默认展示本地语言
                  locale: const Locale('zh', 'CN'),
                  // 备用语言
                  fallbackLocale: const Locale('en', 'US'),
                  home: const MyHomePage(),
                  builder: (BuildContext ctx, Widget? widget) {
                    Widget current = widget!;
                    if (false) {
                      current = Stack(
                        children: [
                          widget,
                          const DebugView(),
                        ],
                      );
                    }
                    return current;
                  },
                  routingCallback: (routing) {
                    // 如果 该路由需要登录，且没token 跳转登录页面
                    // if (indexRouter.authRouters.contains(routing?.current) &&
                    //     hasNotToken) {
                    //   return LoginPage();
                    // }
                    logs('当前路由页面${routing?.current}');
                  },
                  unknownRoute: GetPage(
                      name: HomeRouter.notFound,
                      page: () => const NotFoundPage()),
                  getPages: genRouter()),
            ),
          ),
        ),
      ),
    );
  }
}
